import { Body, Controller, Get, Post, Query, Request, UploadedFile, UseGuards, UseInterceptors } from "@nestjs/common";
import { FileInterceptor } from "@nestjs/platform-express";
import { AuthGuard } from "@nestjs/passport";
import { UploadAddDto } from "./dto/upload-add.dto";
import { UploadService } from "./upload.service";
import { GetFileDto } from "./dto/get-file.dto";

@UseGuards(AuthGuard("jwt"))
@Controller("upload")
export class UploadController {
  constructor(
    private readonly uploadService: UploadService
  ) {
  }

  @Post("add")
  @UseInterceptors(FileInterceptor("files"))
  async doAdd(@Request() req, @Body() body: UploadAddDto, @UploadedFile() file) {
    if (body.useType === "avatar") {
      return await this.uploadService.userAvatarUpload(req.user, body, file);
    }
    return "上传图片成功";
  }

  @Get("getFile")
  async getFile(@Query()dto:GetFileDto) {
    return await this.uploadService.getFile(dto);
  }
}
